use std::collections::HashMap;

use model_graph_types::{
    database::{ConnectorProtocol, DataSource as DatabaseDataSource, DatabaseType},
    datasource::DataSourceConnector,
    modeling::{
        application::ApplicationConversation, set_system_meta_database, ClassGetter, Value,
    },
};

use datasource_connector::connector::SimpleDataSourceConnector;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::DEBUG)
        .init();
    tracing::info!("RRAI 启动...");

    set_system_meta_database(DatabaseDataSource {
        id: 0,
        database_type: DatabaseType::SQLite,
        protocol: ConnectorProtocol::SQLite,
        url: String::from("sqlite:/Users/jinisu/rrg01/rrg01.sqlite"),
        ..Default::default()
    })?;

    let datasource2 = DatabaseDataSource {
        id: 1,
        database_type: DatabaseType::SQLite,
        protocol: ConnectorProtocol::SQLite,
        url: String::from("sqlite:/Users/jinisu/rrg01/app.sqlite"),
        ..Default::default()
    };
    //
    let mut values = HashMap::new();
    values.insert(
        String::from("application_identifier"),
        Value::String(String::from("application_identifier")),
    );
    values.insert(
        String::from("identifier"),
        Value::String(String::from("identifier")),
    );

    let res = SimpleDataSourceConnector::default()
        .instance_insert(
            &None,
            &datasource2,
            &ApplicationConversation::get_identifier()?,
            values,
            None,
        )
        .await;

    match res {
        Ok(response) => {
            tracing::debug!("{}", serde_json::to_string(&response).expect(""));
        }
        Err(err) => {
            tracing::error!("{:?}", err);
        }
    }

    tokio::select! {
        _ = tokio::signal::ctrl_c() => {
            println!();
        }
    };
    tracing::info!("RRAI 结束...");
    Ok(())
}
